//
//  DKFaqSourchCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/11.
//

#import "DKFaqSourchCell.h"

static NSString * FaqSourchCellTableId = @"FaqSourchCellTableId";
@interface DKFaqSourchCell()
@property (nonatomic, strong)UILabel * faqSourchQuestionLab;
@property (nonatomic, strong)UILabel * faqSourchAnswerLab;
@property (nonatomic, strong)UIButton * faqSourchDownBtn;
@property (nonatomic, strong)UILabel * faqSourchThankLab;
@property (nonatomic, strong)UIButton * faqSourchUnresolvedBtn;
@property (nonatomic, strong)UIButton * faqSourchResolvedBtn;
@property (nonatomic, strong)UIView * faqSourchResolvedLineView;

@end
@implementation DKFaqSourchCell
+ (instancetype)faqSourchCell:(UITableView *)tableView{
    DKFaqSourchCell * faqSourchCell = [tableView dequeueReusableCellWithIdentifier:FaqSourchCellTableId];
    if (!faqSourchCell) {
        faqSourchCell = [[DKFaqSourchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FaqSourchCellTableId];
    }
    faqSourchCell.backgroundColor = [UIColor whiteColor];
    faqSourchCell.selectionStyle = UITableViewCellSelectionStyleNone;
    faqSourchCell.accessoryType = UITableViewCellAccessoryNone;
    return faqSourchCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView{
    self.faqSourchQuestionLab = [[UILabel alloc] init];
    self.faqSourchQuestionLab.textColor = DK_Hex(@"#333333");
    self.faqSourchQuestionLab.font = DK_FONT_Medium(15);
    self.faqSourchQuestionLab.numberOfLines = 0;
    [self.contentView addSubview:self.faqSourchQuestionLab];
    [self.faqSourchQuestionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(55)));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.faqSourchDownBtn = [[UIButton alloc] init];
    [self.faqSourchDownBtn setBackgroundImage:DK_GetImage(@"dk_MeFaq_Ping") forState:UIControlStateNormal];
    [self.faqSourchDownBtn setBackgroundImage:DK_GetImage(@"dk_MeFaq_Down") forState:UIControlStateSelected];
    [self.faqSourchDownBtn addTarget:self action:@selector(faqSourchDown) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.faqSourchDownBtn];
    [self.faqSourchDownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.width.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    self.faqSourchAnswerLab = [[UILabel alloc] init];
    self.faqSourchAnswerLab.textColor = DK_Hex(@"#999999");
    self.faqSourchAnswerLab.font = DK_FONT_Regular(12);
    self.faqSourchAnswerLab.numberOfLines = 0;
    [self.contentView addSubview:self.faqSourchAnswerLab];
    [self.faqSourchAnswerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faqSourchQuestionLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(55)));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    self.faqSourchThankLab = [[UILabel alloc] init];
    self.faqSourchThankLab.textColor = DK_Hex(@"#1D1D1D");
    self.faqSourchThankLab.font = DK_FONT_Semibold(12);
    self.faqSourchThankLab.text = @"Thanks for your feedback!";
    self.faqSourchThankLab.textAlignment = NSTextAlignmentCenter;
    self.faqSourchThankLab.hidden = YES;
    [self.contentView addSubview:self.faqSourchThankLab];
    [self.faqSourchThankLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faqSourchAnswerLab.mas_bottom).offset(DK_IsWidthScale(18));
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(18)));
    }];
    
    self.faqSourchUnresolvedBtn = [[UIButton alloc] init];
    [self.faqSourchUnresolvedBtn setImage:DK_GetImage(@"dk_MeFaq_UnresolvedNoSelect") forState:UIControlStateNormal];
    [self.faqSourchUnresolvedBtn setImage:DK_GetImage(@"dk_MeFaq_UnresolvedSelect") forState:UIControlStateSelected];
    [self.faqSourchUnresolvedBtn setTitleColor:DK_Hex(@"#B7B7B7") forState:UIControlStateNormal];
    [self.faqSourchUnresolvedBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateSelected];
    [self.faqSourchUnresolvedBtn setTitle:@" Unresolved" forState:UIControlStateNormal];
    [self.faqSourchUnresolvedBtn setTitle:@" Unresolved" forState:UIControlStateSelected];
    [self.faqSourchUnresolvedBtn setBackgroundColor:DK_Hex(@"#F8F8F8")];
    self.faqSourchUnresolvedBtn.titleLabel.font = DK_FONT_Regular(12);
    self.faqSourchUnresolvedBtn.layer.cornerRadius = DK_IsWidthScale(19);
    [self.faqSourchUnresolvedBtn addTarget:self action:@selector(faqSourchUnresolved) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.faqSourchUnresolvedBtn];
    [self.faqSourchUnresolvedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faqSourchThankLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.left.equalTo(@(DK_IsWidthScale(23)));
        make.width.equalTo(@(DK_IsWidthScale(140)));
        make.height.equalTo(@(DK_IsWidthScale(38)));
    }];
    self.faqSourchResolvedBtn = [[UIButton alloc] init];
    [self.faqSourchResolvedBtn setImage:DK_GetImage(@"dk_MeFaq_ResolvedNoSelect") forState:UIControlStateNormal];
    [self.faqSourchResolvedBtn setImage:DK_GetImage(@"dk_MeFaq_ResolvedSelect") forState:UIControlStateSelected];
    [self.faqSourchResolvedBtn setTitleColor:DK_Hex(@"#B7B7B7") forState:UIControlStateNormal];
    [self.faqSourchResolvedBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateSelected];
    [self.faqSourchResolvedBtn setTitle:@" Resolved" forState:UIControlStateNormal];
    [self.faqSourchResolvedBtn setTitle:@" Resolved" forState:UIControlStateSelected];
    [self.faqSourchResolvedBtn setBackgroundColor:DK_Hex(@"#F8F8F8")];
    self.faqSourchResolvedBtn.titleLabel.font = DK_FONT_Regular(12);
    self.faqSourchResolvedBtn.layer.cornerRadius = DK_IsWidthScale(19);
    [self.faqSourchResolvedBtn addTarget:self action:@selector(faqSourchResolved) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.faqSourchResolvedBtn];
    [self.faqSourchResolvedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faqSourchUnresolvedBtn);
        make.right.equalTo(@(-DK_IsWidthScale(23)));
        make.width.equalTo(@(DK_IsWidthScale(140)));
        make.height.equalTo(self.faqSourchUnresolvedBtn);
        make.bottom.equalTo(@(-DK_IsWidthScale(16)));
    }];
    self.faqSourchResolvedLineView = [[UIView alloc] init];
    self.faqSourchResolvedLineView.backgroundColor = DK_Hex(@"#E0E0E0");
    [self.contentView addSubview:self.faqSourchResolvedLineView];
    [self.faqSourchResolvedLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.height.equalTo(@(DK_IsWidthScale(0.5)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
}
-(void)setFaqTypeMdl:(DKFaqTypeModel *)faqTypeMdl{
    _faqTypeMdl = faqTypeMdl;
    if(_faqTypeMdl){
        NSMutableAttributedString * faqSourchtAnswerAttritedString = [[[NSAttributedString alloc]initWithData:[_faqTypeMdl.answer dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil] mutableCopy];
        [faqSourchtAnswerAttritedString addAttributes:@{NSFontAttributeName:DK_FONT_Regular(12)} range:NSMakeRange(0, faqSourchtAnswerAttritedString.length)];
        if(_faqTypeMdl.isDown){
            CGFloat faqSourchtAnswerH = ceilf([NSString getContentAttributedSize:faqSourchtAnswerAttritedString withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), CGFLOAT_MAX)].height+1);
            
            [self.faqSourchAnswerLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(faqSourchtAnswerH));
            }];
//            [self.faqSourchUnresolvedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.faqSourchThankLab.mas_bottom).offset(DK_IsWidthScale(20));
//                make.height.equalTo(@(DK_IsWidthScale(38)));
//            }];
            self.faqSourchUnresolvedBtn.hidden = NO;
            self.faqSourchResolvedBtn.hidden = NO;
            if(_faqTypeMdl.isUnresolved){
                [self.faqSourchUnresolvedBtn setBackgroundColor:DK_Hex(@"#4566FF")];
            }else{
                self.faqSourchUnresolvedBtn.backgroundColor = DK_Hex(@"#E0E0E0");
            }
            if(_faqTypeMdl.isResolved){
                [self.faqSourchResolvedBtn setBackgroundColor:DK_Hex(@"#4566FF")];
                self.faqSourchThankLab.hidden = NO;
                [self.faqSourchThankLab mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.faqSourchAnswerLab.mas_bottom).offset(DK_IsWidthScale(18));
                    make.height.equalTo(@(DK_IsWidthScale(18)));
                }];
                [self.faqSourchUnresolvedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.faqSourchThankLab.mas_bottom).offset(DK_IsWidthScale(0));
                    make.height.equalTo(@(DK_IsWidthScale(0)));
                }];
                self.faqSourchUnresolvedBtn.hidden = YES;
                self.faqSourchResolvedBtn.hidden = YES;
            }else{
                self.faqSourchResolvedBtn.backgroundColor = DK_Hex(@"#E0E0E0");
                self.faqSourchThankLab.hidden = YES;
                [self.faqSourchThankLab mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.faqSourchAnswerLab.mas_bottom).offset(DK_IsWidthScale(0));
                    make.height.equalTo(@(DK_IsWidthScale(0)));
                }];
                [self.faqSourchUnresolvedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.faqSourchThankLab.mas_bottom).offset(DK_IsWidthScale(20));
                    make.height.equalTo(@(DK_IsWidthScale(38)));
                }];
            }
        }else{
            [self.faqSourchAnswerLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
            [self.faqSourchUnresolvedBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.faqSourchThankLab.mas_bottom).offset(DK_IsWidthScale(0));
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
            [self.faqSourchThankLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.faqSourchAnswerLab.mas_bottom).offset(DK_IsWidthScale(0));
                make.height.equalTo(@(DK_IsWidthScale(0)));
            }];
            self.faqSourchThankLab.hidden = YES;
            self.faqSourchUnresolvedBtn.hidden = YES;
            self.faqSourchResolvedBtn.hidden = YES;
        }
        NSMutableAttributedString * faqSourchtQuestionAttritedString = [[[NSAttributedString alloc]initWithData:[_faqTypeMdl.question dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil] mutableCopy];
        [faqSourchtQuestionAttritedString addAttributes:@{NSFontAttributeName:DK_FONT_Medium(15)} range:NSMakeRange(0, faqSourchtQuestionAttritedString.length)];
        CGFloat faqSourchtQuestionH = ceilf([NSString getContentAttributedSize:faqSourchtQuestionAttritedString withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(100), CGFLOAT_MAX)].height+1);
        
//        CGFloat faqSourchtQuestionH = ceilf([NSString getContentSize:_faqTypeMdl.question withFont:DK_FONT_Medium(15) withSize:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(100), CGFLOAT_MAX)].height);
        [self.faqSourchQuestionLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(faqSourchtQuestionH));
        }];
        
        _faqSourchQuestionLab.attributedText = faqSourchtQuestionAttritedString;
        _faqSourchAnswerLab.attributedText = faqSourchtAnswerAttritedString;
        _faqSourchDownBtn.selected = _faqTypeMdl.isDown;
        _faqSourchUnresolvedBtn.selected = _faqTypeMdl.isUnresolved;
        _faqSourchResolvedBtn.selected = _faqTypeMdl.isResolved;
        _faqSourchResolvedLineView.hidden = _isEnd;
    }
}
-(void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    if(_isEnd){
        [self makeCornerOnBottomleftRight:DK_IsWidthScale(10)];
    }else{
        [self makeCornerOnBottomleftRight:DK_IsWidthScale(0)];
    }
}
- (void)faqSourchDown{
    if(self.UpdataTableBlock){
        self.UpdataTableBlock(self.indexRow);
    }
}
- (void)faqSourchUnresolved{
    if(self.UpdataUnresolvedBlock){
        self.UpdataUnresolvedBlock(self.indexRow);
    }
}
- (void)faqSourchResolved{
    if(self.UpdataResolvedBlock){
        self.UpdataResolvedBlock(self.indexRow);
//        [DKAlertHelper showMessage:@"Thanks for your feedback!"];
    }
}
@end
